package com.aitaoxue.palx.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aitaoxue.palx.R;


/**
 * application基类
 * 提供pref保存数据的静态方法
 * 
 * @author earthgee
 */
public class BaseApplication extends Application {
	private static String PREF_NAME = "aitaoxue.pref";   //pref文件名
	static Context _context;
	static Resources _resources;
	private static String lastToast = "";
	private static long lastToastTime;

	/**
	 * 初始化context和resources
	 * 
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		_context = getApplicationContext();  //全局context
		_resources = _context.getResources();
	}

	/**
	 * 同步获取context
	 * 
	 * @return BaseApplication
	 */
	public static synchronized BaseApplication context() {
		return (BaseApplication) _context;
	}

	/**
	 * 获取resources
	 * 
	 * @return Resources
	 */
	public static Resources resources() {
		return _resources;
	}

	/**
	 * get pref named aitaoxue pref for private
	 * 
	 * @return SharedPreferences
	 */
	public static SharedPreferences getPersistPreferences() {
		return context().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
	}

	/**
	 * 提交修改pref文件
	 * 
	 * @param editor
	 * 
	 */
	public static void apply(SharedPreferences.Editor editor) {
		editor.apply();
	}

	/**
	 * 跨进程访问pref
	 * 
	 * @return
	 */
	public static SharedPreferences getPreferences() {
		return context().getSharedPreferences(PREF_NAME,
				Context.MODE_MULTI_PROCESS);
	}

	/**
	 * 获得pref里保存的屏幕参数
	 * 
	 * @return int[]
	 * 0. width
	 * 1. height
	 * 
	 */
	public static int[] getDisplaySize() {
		return new int[] { getPreferences().getInt("screen_width", 480),
				getPreferences().getInt("screen_height", 854) };
	}

	/**
	 * 保存屏幕参数
	 * 
	 * @param activity
	 */
	public static void saveDisplaySize(Activity activity) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay()
				.getMetrics(displayMetrics);
		SharedPreferences.Editor editor = getPreferences().edit();
		editor.putInt("screen_width", displayMetrics.widthPixels);
		editor.putInt("screen_height", displayMetrics.heightPixels);
		editor.putFloat("density", displayMetrics.density);
		editor.commit();
	}

	/**
	 * 根据资源id获得字符串
	 * 
	 * @param id
	 * 资源id
	 * 
	 * @return String
	 */
	public static String string(int id) {
		return _resources.getString(id);
	}

	/**
	 * 根据资源id获得字符串
	 * 
	 * @param id
	 * @param args
	 * @return String
	 */
	public static String string(int id, Object... args) {
		return _resources.getString(id, args);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * 资源id
	 * 
	 */
	public static void showToast(int message) {
		showToast(message, Toast.LENGTH_LONG, 0);
	}

	/**
	 * toast
	 * 
	 * @param message
	 */
	public static void showToast(String message) {
		showToast(message, Toast.LENGTH_LONG, 0, 17);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * @param icon
	 * 图标
	 */
	public static void showToast(int message, int icon) {
		showToast(message, Toast.LENGTH_LONG, icon);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * @param icon
	 */
	public static void showToast(String message, int icon) {
		showToast(message, Toast.LENGTH_LONG, icon, Gravity.BOTTOM);
	}

	/**
	 * toast(短时间)
	 * 
	 * @param message
	 */
	public static void showToastShort(int message) {
		showToast(message, Toast.LENGTH_SHORT, 0);
	}

	/**
	 * toast(短时间)
	 * 
	 * @param message
	 */
	public static void showToastShort(String message) {
		showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
	}

	/**
	 * toast(短时间)
	 * 
	 * @param message
	 * @param args
	 */
	public static void showToastShort(int message, Object... args) {
		showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM, args);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * @param duration
	 * 自定义间隔
	 * @param icon
	 */
	public static void showToast(int message, int duration, int icon) {
		showToast(message, duration, icon, Gravity.BOTTOM);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * @param duration
	 * @param icon
	 * @param gravity
	 */
	public static void showToast(int message, int duration, int icon,
			int gravity) {
		showToast(context().getString(message), duration, icon, gravity);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * @param duration
	 * @param icon
	 * @param gravity
	 * @param args
	 */
	public static void showToast(int message, int duration, int icon,
			int gravity, Object... args) {
		showToast(context().getString(message, args), duration, icon, gravity);
	}

	/**
	 * toast
	 * 
	 * @param message
	 * @param duration
	 * @param icon
	 * @param gravity
	 */
	public static void showToast(String message, int duration, int icon,
			int gravity) {
		if (message != null && !message.equalsIgnoreCase("")) {
			long time = System.currentTimeMillis();
			if (!message.equalsIgnoreCase(lastToast)
					|| Math.abs(time - lastToastTime) > 2000) {
				View view = LayoutInflater.from(context()).inflate(
						R.layout.view_toast, null);
				((TextView) view.findViewById(R.id.title_tv)).setText(message);
				if (icon != 0) {
					((ImageView) view.findViewById(R.id.icon_iv))
							.setImageResource(icon);
					((ImageView) view.findViewById(R.id.icon_iv))
							.setVisibility(View.VISIBLE);
				}
				Toast toast = new Toast(context());
				toast.setView(view);
				if (gravity == Gravity.CENTER) {
					toast.setGravity(gravity, 0, 0);
				} else {
					toast.setGravity(gravity, 0, 35);
				}

				toast.setDuration(duration);
				toast.show();
				lastToast = message;
				lastToastTime = System.currentTimeMillis();
			}
		}
	}
}
